/*
Copyright (C) 2015 Marien Raat

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

// This class takes one dimensional double array as a heightmap and a
// double as the water level.
// It returns a one dimensional array of booleans where true signifies
// land and false signifies sea.
// Lakes are not generated by this class

// Note: The heightmap and the water level should be normalized
// between 0.0 and 1.0 for this class.
class LandMapGenerator {
public:
    LandMapGenerator();

    // Sets the water level
    void setWaterLevel(double waterLevel);

    // Returns a pointer to this->landMap
    bool *generateLandMap(
        // The heightmap array to derive the land map from.
        // Normalized between 0.0 and 1.0.
        double *heightmap,
        // The width of the heightmap
        unsigned width,
        // The height of the heightmap
        unsigned height,
        // If the current landMap should be overwritten or a new one
        // should be created
        bool overwriteOldMap = true);

    // Returns a pointer to this->surfaceHeightmap
    double *generateSurfaceHeightmap(
        // The heightmap array to derive the surface height map from.
        // Normalized between 0.0 and 1.0.
        double *heightmap,
        // The width of the heightmap
        unsigned width,
        // The height of the heightmap
        unsigned height,
        // If the current surfaceHeightMap and landMap should be
        // overwritten or a new one should be created.
        bool overwriteOldMap = true);

    // A pointer to an one dimensional boolean array of the same size
    // as the heightmap. true signifies land, and lakes while false
    // signifies sea.
    // NOTE: Will only contain valid data if generateLandMap has been
    // called
    // Lakes are now also on land but are not covered by this map, any
    // true value can be land, lake or river.
    bool *landMap;

    // A pointer to an one dimensional double array of the same size
    // as the heightmap. The values show the height of the
    // surface. This means that on places which are determined to be
    // land the value is the same as the heightmap, but on places
    // which arent the value is equal to the waterLevel.
    // NOTE: Will only contain valid dat if generateSurfaceHeightMap
    // has been called.
    double *surfaceHeightmap;
private:
    double waterLevel;
    bool firstLandMap, firstSurfaceHeightmap;
    int previousWidthLandMap, previousHeightLandMap,
        previousWidthHeightmap, previousHeightHeightmap;

    // Writes to the points adjacent to (x, y) if they are part of the
    // sea, when (x, y) is part of the sea.
    // Returns true to signify that it found at least one new part of
    // sea. Returns false if it failed to find a new point of sea.
    bool spreadFrom(
        // x and y coordinates to spread from
        unsigned x,
        unsigned y,
        // The width and height of the belowSeaLevelMap and the
        // landMap
        unsigned width,
        unsigned height,
        // The map that signifies whether a point is below sea level
        bool *belowSeaLevel,
        // The map to write to which points are below sea level.
        bool *landMap);
};
